/*
 * 
 * 
 * 
 */
package com.hboxs.asl.entity;

import com.hboxs.asl.util.DateUtils;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Entity - 私信
 */
@Entity
@Table(name = "xx_letter")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_letter_sequence")
public class Letter extends BaseEntity {

    private static final long serialVersionUID = -5035341234322850722L;

    /**
     * 内容
     */
    private String content;

    /**
     * 发件人
     */
    private Member sender;

    /**
     * 收件人
     */
    private Member receiver;

    /**
     * 原消息
     */
    private Letter forLetter;

    /**
     * 是否已读
     */
    private Boolean receiverRead;

    /**
     * 回复消息
     */
    private Set<Letter> replyLetteres = new HashSet<>();

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 获取发件人
     *
     * @return 发件人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(updatable = false)
    public Member getSender() {
        return sender;
    }

    public void setSender(Member sender) {
        this.sender = sender;
    }

    /**
     * 获取收件人
     *
     * @return 发件人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(updatable = false)
    public Member getReceiver() {
        return receiver;
    }

    public void setReceiver(Member receiver) {
        this.receiver = receiver;
    }

    /**
     * 获取原消息
     *
     * @return 原消息
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(updatable = false)
    public Letter getForLetter() {
        return forLetter;
    }

    /**
     * 设置原消息
     *
     * @param forLetter 原消息
     */
    public void setForLetter(Letter forLetter) {
        this.forLetter = forLetter;
    }

    /**
     * 获取回复消息
     *
     * @return 回复消息
     */
    @OneToMany(mappedBy = "forLetter", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    @OrderBy(value = "createDate asc")
    public Set<Letter> getReplyLetteres() {
        return replyLetteres;
    }

    /**
     * 设置回复消息
     *
     * @param replyLetteres 回复消息
     */
    public void setReplyLetteres(Set<Letter> replyLetteres) {
        this.replyLetteres = replyLetteres;
    }

    public Boolean getReceiverRead() {
        return receiverRead;
    }

    public void setReceiverRead(Boolean receiverRead) {
        this.receiverRead = receiverRead;
    }

    @Transient
    public String getCreateDateF(){
        return DateUtils.dateToStr(super.getCreateDate());
    }
}